package com.holly.unit.deform.modular.controller;
import com.holly.unit.core.pojo.response.ErrorResponseData;
import com.holly.unit.core.pojo.response.ResponseData;
import com.holly.unit.core.pojo.response.SuccessResponseData;
import com.holly.unit.deform.api.DeformDataApi;
import com.holly.unit.deform.api.DeformExtLinkApi;
import com.holly.unit.deform.api.pojo.rsp.DeformResult;
import com.holly.unit.deform.entity.DesignFormData;
import com.holly.unit.scanner.api.annotation.ApiResource;
import com.holly.unit.scanner.api.annotation.GetResource;
import com.holly.unit.scanner.api.annotation.PostResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 类描述: 表单设计器外部链接 控制器
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/4/12 11:19
 */
@RestController
@ApiResource(name = "表单设计器外部链接")
@Api(tags = "表单设计器外部链接")
public class DeformExtLinkController {

    @Resource
    private DeformExtLinkApi<ModelAndView> api;

    @Resource
    private DeformDataApi<DesignFormData> dataApi;

    /**
     * 外部表单新增页面地址
     */
    @ApiOperation(value = "外部表单新增页面地址" ,notes = "外部表单新增页面地址")
    @GetResource(name = "外部表单新增页面地址" , path = "/desformExt/{desformCode}")
    public ModelAndView externalAddDesformData(@PathVariable("desformCode") String desformCode, @RequestParam(name = "reuseId", required = false) String reuseId, ModelAndView modelAndView, HttpServletRequest request) {

        return api.externalAddDesformData(desformCode,reuseId,modelAndView,request);
    }

    /**
     * 外部表单修改页面地址
     */
    @ApiOperation(value = "外部表单修改页面地址" ,notes = "外部表单修改页面地址")
    @GetResource(name = "外部表单修改页面地址" , path = "/desformExt/{desformCode}/{dataId}")
    public ModelAndView externalEditDesformData(@PathVariable("desformCode") String desformCode, @PathVariable("dataId") String dataId, ModelAndView modelAndView, HttpServletRequest request) {

        return api.externalEditDesformData(desformCode,dataId,modelAndView,request);
    }

    /**
     * 外部链接新增接口，不需要Token验证
     *
     * @param desformCode
     * @param designFormData
     * @return
     */
    @ApiOperation(value = "外部链接新增接口" ,notes = "外部链接新增接口")
    @PostResource(name = "外部链接新增接口" , path = "/desformExt/{desformCode}")
    public ResponseData extAdd(@PathVariable("desformCode") String desformCode, @RequestBody DesignFormData designFormData) {

        DeformResult deformResult = dataApi.extAdd(desformCode,designFormData);
        if (deformResult.isSuccess()) {
            return new SuccessResponseData();
        }
        return new ErrorResponseData(deformResult.getCode().toString(),deformResult.getMessage(),deformResult.getResult());
    }

    /**
     * 外部链接修改接口，不需要Token验证
     */
    @ApiOperation(value = "外部链接修改接口" ,notes = "外部链接修改接口")
    @PostResource(name = "外部链接修改接口" , path = "/desformExt/{desformCode}/{dataId}")
    public ResponseData extEdit(@PathVariable("desformCode") String desformCode, @PathVariable("dataId") String dataId, @RequestBody DesignFormData designFormData) {
        DeformResult deformResult = dataApi.extEdit(desformCode, designFormData);
        if (deformResult.isSuccess()) {
            return new SuccessResponseData();
        }
        return new ErrorResponseData(deformResult.getCode().toString(),deformResult.getMessage(),deformResult.getResult());
    }

    /**
     * 请求成功后返回的页面
     */
    @ApiOperation(value = "请求成功后返回的页面" ,notes = "请求成功后返回的页面")
    @GetResource(name = "请求成功后返回的页面" , path = "/desformExt/success")
    public ModelAndView doSuccess(@RequestParam("desformCode") String desformCode, @RequestParam("dataId") String dataId, ModelAndView mv, HttpServletRequest request) {
        return api.doSuccess(desformCode,dataId,mv,request);
    }

}
